import java.util.ArrayList;
import java.util.Scanner;
public class Product {
    private String code;
    private String goodsName;
    private String factory;
    private String goodsDate;
    private String model;
    private double price;
    private double sellPrice;
    private int num;
    public Product(){}
    public Product(String code, String goodsName, String factory, String goodsDate, String model, double price, double sellPrice, int num) {
        this.code = code;
        this.goodsName = goodsName;
        this.factory = factory;
        this.goodsDate = goodsDate;
        this.model = model;
        this.price = price;
        this.sellPrice = sellPrice;
        this.num = num;
    }

    public void listGoodsInfo(ArrayList<Product> goodsList) {
        for (Product product : goodsList) {
            System.out.println("商品编号：" + product.getCode() + "   商品名称：" + product.getGoodsName() + "   生产产家：" + product.getFactory() + "   生产日期" + product.getGoodsDate() + "   型号：" + product.getModel() + "   进货价：" + product.getPrice() + "   零售价：" + product.getSellprice() + "   商品数量：" + product.getNum());
        }
    }

    public void addInfo(ArrayList<Product> goodsList) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入添加的商品编号：");
        String code = reader.next();
        System.out.println("请输入添加的商品名称：");
        String goodsName = reader.next();
        System.out.println("请输入添加的生产产家：");
        String factory = reader.next();
        System.out.println("请输入添加的生产日期;");
        String goodsDate = reader.next();
        System.out.println("请输入添加的商品型号：");
        String model = reader.next();
        System.out.println("请输入添加的进货价：");
        double price = reader.nextDouble();
        System.out.println("请输入添加的零售价：");
        double sellPrice = reader.nextDouble();
        System.out.println("请输入添加的商品数量：");
        int num = reader.nextInt();
        goodsList.add(new Product(code, goodsName, factory, goodsDate, model, price, sellPrice, num));
        System.out.println("添加商品成功！");
    }

    public void changeGoodsInfo(ArrayList<Product> goodsList) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入您想修改的商品编号");
        String CODE = reader.next();
        for (Product product : goodsList) {
            if (CODE.equals(product.code)) {
                int index = goodsList.indexOf(product);
                System.out.println("请选择需要修改的商品信息：");
                System.out.println("1、商品编号");
                System.out.println("2、商品名称");
                System.out.println("3、生产厂家");
                System.out.println("4、生产日期");
                System.out.println("5、产品型号");
                System.out.println("6、进货价");
                System.out.println("7、零售价");
                System.out.println("8、商品数量");
                int choice = reader.nextInt();
                switch (choice) {
                    case 1: {
                        System.out.println("请输入新的商品编号：");
                        product.code = reader.next();
                        goodsList.set(index, product);
                        break;
                    }
                    case 2: {
                        System.out.println("请输入新的商品名称：");
                        product.goodsName = reader.next();
                        goodsList.set(index, product);
                        break;
                    }
                    case 3: {
                        System.out.println("请输入新的生产产家：");
                        product.factory = reader.next();
                        goodsList.set(index, product);
                        break;
                    }
                    case 4: {
                        System.out.println("请输入新的生产日期：");
                        product.goodsDate = reader.next();
                        goodsList.set(index, product);
                        break;
                    }
                    case 5: {
                        System.out.println("请输入新的产品型号：");
                        product.model = reader.next();
                        goodsList.set(index, product);
                        break;
                    }
                    case 6: {
                        System.out.println("请输入新的进货价：");
                        product.price = reader.nextDouble();
                        goodsList.set(index, product);
                        break;
                    }
                    case 7: {
                        System.out.println("请输入新的零售价：");
                        product.sellPrice = reader.nextDouble();
                        goodsList.set(index, product);
                        break;
                    }
                    case 8: {
                        System.out.println("请输入新的商品数量：");
                        product.num = reader.nextInt();
                        goodsList.set(index, product);
                        break;
                    }
                }
                System.out.println("是否继续修改？");
                System.out.println("1、是  2、否");
                Scanner input = new Scanner(System.in);
                int choice1 = input.nextInt();
                switch (choice) {
                    case 1:
                        changeGoodsInfo(goodsList);
                        break;
                    case 2:
                        break;
                }
            } else {
                System.err.println("商品名不正确或商品不存在，请重试。");
                changeGoodsInfo(goodsList);
            }
        }
    }
    public void delGoodsInfo(ArrayList<Product>goodsList){
        System.out.println("请输入您想删除的商品编号：");
        Scanner reader = new Scanner(System.in);
        int m=0;
        String CODE=reader.next();
        for(Product product:goodsList){
            if(CODE.equals(product.getCode())){
                System.out.println("请再次确认您是否要删除该商品信息（yes or no）");
                String isYes=reader.next();
                if(isYes.equals("yes")){
                    goodsList.remove(m);
                    System.out.println("您已成功删除该商品的信息！");
                    break;
                }
            }
            m++;
        }
    }
    public void findGoodsInfo(ArrayList<Product>goodsList){
        System.out.println("请输入您想查询商品的商品编号：");
        Scanner reader = new Scanner(System.in);
        String CODE=reader.next();
        for(int i=0;i<goodsList.size();i++){
            if(CODE.equals(goodsList.get(i).getCode())){
                System.out.println(goodsList.get(i));
            }
        }
        /*int m=0;
        for(Product product:goodsList){
            if(CODE.equals(product.getCode())){
                System.out.println(goodsList.get(m));
            }
            m++;
        }*/
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String proname) {
        this.goodsName = proname;
    }

    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }

    public String getGoodsDate() {
        return goodsDate;
    }

    public void setProDate(String proDate) {
        this.goodsDate = proDate;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    public double getSellprice() {
        return sellPrice;
    }
    public void setSellprice(double sellprice) {
        this.sellPrice = sellprice;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    @Override
    public String toString() {
        return "商品名称: " + goodsName + ", 价格: " + price + ", 数量: " + num;
    }
}

